<script setup>
import HelloWorld from './components/HelloWorld.vue'
import { ref, onMounted, onUnmounted } from 'vue'
import { NInput, NButton } from 'naive-ui'
import { useMessage } from 'naive-ui'

const message = useMessage()

// 收入计算相关
const incomeInput = ref('')
const totalIncome = ref(0)

// 输入校验函数
const validateInput = (value) => {
  return !isNaN(value) && parseFloat(value) >= 0
}

// 计算收入
const calculateIncome = () => {
  if (validateInput(incomeInput.value)) {
    totalIncome.value += parseFloat(incomeInput.value)
    incomeInput.value = ''
    startCoinAnimation()
  } else {
    message.error('请输入有效的正数金额')
  }
}

// 定时计算（每 5 秒）
let timer
onMounted(() => {
  timer = setInterval(() => {
    // 这里可以添加定时计算逻辑
    console.log('定时计算执行中，当前总收入: ', totalIncome.value)
  }, 5000)
})
onUnmounted(() => {
  clearInterval(timer)
})

// 金币动画函数
const startCoinAnimation = () => {
  // 这里可以添加金币动画逻辑
  console.log('金币动画启动')
}
</script>

<template>
  <n-message-provider>
    <div>
      <a href="https://vite.dev" target="_blank">
        <img src="/vite.svg" class="logo" alt="Vite logo" />
      </a>
      <a href="https://vuejs.org/" target="_blank">
        <img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
      </a>
      <div>
        <NInput v-model="incomeInput" placeholder="请输入收入金额" />
        <NButton @click="calculateIncome">添加收入</NButton>
        <p>总收入: {{ totalIncome }}</p>
      </div>
      <HelloWorld msg="Vite + Vue" />
    </div>
  </n-message-provider>
</template>

<style scoped>
.logo {
  height: 6em;
  padding: 1.5em;
  will-change: filter;
  transition: filter 300ms;
}
.logo:hover {
  filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
  filter: drop-shadow(0 0 2em #42b883aa);
}
</style>
